using System;
using System.Data;
using System.Windows.Forms;
using BLL;
using Common;
using Model;

namespace UI {
    public partial class FrmUserInfoOperation : Form {
        private FrmManagerLogin _frmManagerLogin;
        private UserBLL _userBll = UserBLL.GetUserInfoBLL();

        public FrmUserInfoOperation() {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        public FrmUserInfoOperation(FrmManagerLogin frmManagerLogin) : this() {
            _frmManagerLogin = frmManagerLogin;
        }
        

        private void FrmUserInfoOperation_Load(object o, EventArgs e) {
            UpdataGrid();
        }

        private void UpdataGrid() {
            DataTable dataTable = _userBll.GetAllUsers();
            dataGridView1.DataSource = dataTable;
        }

        //双击查看具体信息
        private void dataGridView1_CellDoubleClick(object o, DataGridViewCellEventArgs e) {
            DataGridViewRow dataGridViewRow = dataGridView1.CurrentRow;
            textBox1.Text = dataGridViewRow.Cells[0].Value.ToString();
            textBox2.Text = dataGridViewRow.Cells[1].Value.ToString();
            textBox3.Text = dataGridViewRow.Cells[2].Value.ToString();
            textBox4.Text = dataGridViewRow.Cells[3].Value.ToString();

            if (dataGridViewRow.Cells[4].Value.ToString() == "员工") {
                radioButton1.Checked = true;
            } else {
                radioButton2.Checked = true;
            }
        }

        //新增员工
        private void button1_Click(object o, EventArgs e) {
            //不严谨地校验数据合法性
            string username = textBox2.Text.Trim();
            string pwd = textBox3.Text.Trim();
            string realname = textBox4.Text.Trim();
            
            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(pwd) || string.IsNullOrEmpty(realname)) {
                MessageBox.Show("username, password and realname are required");
                return;
            }
            
            Result<User> result = _userBll.AddUser(new User() {
                UserName = username,
                Password = pwd,
                RealName = realname,
                Position = radioButton1.Checked ? "员工" : "经理"
            });
            
            if (result.IsSuccess) {
                UpdataGrid();
            }
            MessageBox.Show(result.Message);
        }

        //更新员工信息
        private void button2_Click(object o, EventArgs e) {
            //不严谨地校验数据合法性
            string username = textBox2.Text.Trim();
            string pwd = textBox3.Text.Trim();
            string realname = textBox4.Text.Trim();
            
            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(pwd) || string.IsNullOrEmpty(realname)) {
                MessageBox.Show("username, password and realname are required");
                return;
            }
            
            Result<User> result = _userBll.UpdateUserInfo(new User() {
                Uid = int.Parse(textBox1.Text),
                UserName = username,
                Password = pwd,
                RealName = realname,
                Position = radioButton1.Checked ? "员工" : "经理"
            });
            
            if (result.IsSuccess) {
                UpdataGrid();
            }
            MessageBox.Show(result.Message);
        }

        //删除用户信息
        private void button3_Click(object o, EventArgs e) {
            Result<User> result = _userBll.DeleteById(new User() {
                Uid = int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString())
            });
            
            if (result.IsSuccess) {
                UpdataGrid();
            }
            MessageBox.Show(result.Message);
        }

        private void FrmUserInfoOperation_FormClosing(object sender, FormClosingEventArgs e) {
            _frmManagerLogin.Close();
        }
    }
}